import Jsonp from 'jsonp';
import axios from 'axios';
import Utils from '@utils/utils.js';

export default class request {
  static BdWeather(options) {
    return new Promise((resolve, reject) => {
      Jsonp(options.url, {
        parma: "callback"
      }, (err, response) => {
        if (!!response&&response.status === 'success') {
          resolve(response.results)
        } else {
          reject(err)
        }
      })
    })
  }

  static axiosRes(options) {

    let loading = null;
    if (options.data&&options.data.showLoading) {
      loading = document.querySelector('#rc-loading');
      loading.style.display = 'block';
    }

    let baseUrl = "https://www.easy-mock.com/mock/5b9e570c2b292b0e9154c614/testdata";
    return new Promise((resolve, reject) => {
      axios({
        baseURL: baseUrl,
        url: options.url,
        method: options.method,
        params: options.params ? options.params : null,
        data: options.data ? options.data : null,
        timeout: options.timeout || 60000,
      }).then((response) => {
        !!loading ? loading.style.display = 'none' : 0;
        if (response.status === 200) {
          let res = response.data;
          resolve(res);
        } else {
          Utils.antMessage('error', response.data.msg);
          reject(response.data);
        }
      }).catch((error) => {
        !!loading ? loading.style.display = 'none' : 0;
        let err = error.response;
        let msg = `请求出错，code：${err.status}， 错误信息：${err.data.error||err.statusText}`;
        Utils.antMessage('error', msg);
        reject(err.data.error||err.statusText);
        // let httpStatus = err.response.status;
        // switch (true) {
        //   case httpStatus>=400:
        //     console.log(window.location.href);
        //     break;
        // }
      });
    });
  }

  
}